﻿/// <reference path="../../StringBuilder.js" />

RegExp.prototype.Replace = function(str, replacementOrFunction)
{
	var sbRes = new StringBuilder();
	while (str.length > 0)
	{
		var match = this.exec(str);
		if (match == null)
			break;

		sbRes.Append(str.substr(0, match.index));
		if (typeof (replacementOrFunction) == "function")
		{
			sbRes.Append(replacementOrFunction(match));
		}
		else
		{
			sbRes.Append(replacementOrFunction);
		}

		str = str.substr(match.index + match[0].length);
	}
	sbRes.Append(str);
	return sbRes.toString();
}
